package com.example.forum.controller;

import com.example.forum.config.AppConfig;
import com.example.forum.exception.ApplicationException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestController {

    /**
     * 测试Spring环境
     *
     * @return
     */
    @GetMapping("/test")
    public String test() {
        return "hello forum!";
    }

    /**
     * 测试处理系统异常
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/exception")
    public String testException() throws Exception {
        throw new Exception(" 这是⼀个 Exception");
    }

    /**
     * 测试处理自定义异常
     *
     * @return
     */
    @GetMapping("/appException")
    public String testApplicationException() {
        throw new ApplicationException(" 这是⼀个⾃定义的 ApplicationException");
    }

    /**
     * 测试Session
     *
     * @param request
     */
    @RequestMapping("/test02")
    public void test02(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session.getAttribute(AppConfig.USER_SESSION) == null) {
            System.out.println("session==null");
        } else {
            System.out.println("session!=null");
        }
    }
}
